


var myTree = [];

showMenus();
function showMenus(){
	$.ajax({
		url:ajax_host+'menu.act',
		type:'post',
    	dataType:'JSON',
    	success:function(resp){
    		
    		if(resp.success){
    			console.log(resp);
    			var list = resp.list;
    			//整理菜单
    			var treeArr = [];
    			parseMenuTree(0,treeArr,list);
    			
    			 myTree = $('#treeMenu').data('zui.tree');
    			 myTree.reload(treeArr);
    		}
    	},
    	error:function(resp){
    		layer.alert('请联系管理员')
    	}
	});
}

//pid:父id，treeArr:整理好的数据存在这个数组中，menuList:菜单集合
function parseMenuTree(pid,treeArr,menuList){
	for(var i = 0;i < menuList.length; i++){
		if(menuList[i].pmenuId == pid){
			var jsonObj = {
				html: `<a href="#" onclick="doSkip('${menuList[i].menuId}','${menuList[i].menuUrl}','${menuList[i].pmenuId}')">${menuList[i].menuName}</a>`,
				children:[]
			};
			//添加到这个集合中
			treeArr.push(jsonObj);
			//判断子菜单
			parseMenuTree(menuList[i].menuId,jsonObj.children,menuList);
		}
	}
}
function doSkip(jid,url,pid){
	console.log(jid +'url的值:'+url+"---------"+"pid的值:"+pid);

	// if(jid==1||jid==2){
	// 	$('#frame').attr("src","page.act?p="+"###");
	// 	return;
	// }else{
	// 	$('#frame').attr("src","page.act?p="+url+"&h="+Math.random());
	// 	return;
	// }

	if(jid>2){
		$('#frame').attr("src","page.act?p="+url+"&h="+Math.random());
	}
	
	
}


// $('#myTree').tree({data: myTreeData});

//2.初始化菜单
$('#treeMenu').tree({
    animate:true,//动画效果
    initialState:'collapse',//初始状态,从本地存储还原用户上次操作状态
    data: myTree//数组，包含所有节点对象，可选
});

//3.数据动态更新
function doChange(){
	// 获取 tree 实例
	console.log('开始刷新菜单')
    //  var myTree = $('#treeMenu').data('zui.tree');

    // // 准备新的数据
    // var myNewTreeData = [...];

    // 更新数据
	// myTree.reload(myTree);
	showMenus();
}

getUserName();
function getUserName(){
	$.ajax({
		url:ajax_host+'getAdminName.act',
    	dataType:'JSON',
    	success:function(resp){
    		if(resp.success){
    			html('adminName',resp.adminname)
    		}else{
				layer.alert('获取用户名失败')
			}
    	},
    	error:function(resp){
    		layer.alert('获取用户名失败')
    	}
	});
}

function doUpdatee(){
    console.log('获取修改遮罩');
    $('#smzz').modal({
        show:true,//展示出来
        moveable:true,//可移动
        rememberPos:true,//记住移动位置
        backdrop:'static'//背景遮罩
    });

}

function confirm(){
  
    var uName = val('uName');
    var uPwd = val('uPwd');
	var cPwd = val('cPwd');
	var uAccount = val('uAccount');
	console.log(uName+" "+uPwd+" "+cPwd+" "+uAccount);

	
    $.ajax({
		url:ajax_host+'adminEditdate.act',
		type : 'post',
		dataType:'JSON',
		data:{
            aName:uName,
            aPwd:uPwd,
			cPwd:cPwd,
			uAccount:uAccount
		},
		success:function(resp){
			
			if(resp.success){
				//关闭遮罩
				$('#smzz').modal('hide', 'fit');
				layer.alert('修改成功');
			}else{
				$('#smzz').modal('hide', 'fit');
				layer.alert(resp.errmsg);
			}
		}
})
}